package com.smf.statement.merge;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.eclipse.core.runtime.Platform;

public class WorkBookManager {
	
	private static final String SHEET = "SHEET";
	private static final String COLUMN = "COLUMN";
	private static final String INPUT_SHEET_END = "INPUTSHEETEND";
	private static final String OUTPUT_SHEET_END = "OUTPUTSHEETEND";
	private static final String ALL_END = "END";
	
	private static List<SheetBD> inputSheets = new ArrayList<SheetBD>();
	private static List<SheetBD> outputSheets = new ArrayList<SheetBD>();
	
	public static void init(String path) throws FileNotFoundException, IOException, URISyntaxException{
		Workbook wb = new HSSFWorkbook(new FileInputStream(path + "\\DEFINITION.xls"));
		Sheet sheet = wb.getSheet("SHEETS_DEFINITION");
		Iterator<Row> rows = sheet.rowIterator();
		SheetBD sheetBD = null;
		while(rows.hasNext()){
			Row currentRow = rows.next();
			Cell type = currentRow.getCell(0);
			if(type == null)
				continue;
			if(SHEET.equalsIgnoreCase(type.getStringCellValue())){
				Cell sheetName = currentRow.getCell(1);
				Cell sheetSeq = currentRow.getCell(2);
				sheetBD = new SheetBD(sheetName.getStringCellValue(), sheetSeq.getNumericCellValue());
			}
			if(COLUMN.equalsIgnoreCase(type.getStringCellValue())){
				Cell columnName = currentRow.getCell(1);
				Cell columnSeq = currentRow.getCell(2);
				Cell columnType = currentRow.getCell(3);
				ColumnBD columnBD= new ColumnBD(columnName.getStringCellValue(), columnSeq.getNumericCellValue(), columnType.getStringCellValue());
				sheetBD.addColumn(columnBD);
			}
			if(INPUT_SHEET_END.equalsIgnoreCase(type.getStringCellValue())){
				inputSheets.add(sheetBD);
			}
			if(OUTPUT_SHEET_END.equalsIgnoreCase(type.getStringCellValue())){
				outputSheets.add(sheetBD);
			}
			if(ALL_END.equalsIgnoreCase(type.getStringCellValue())){
				break;
			}
		}
		System.out.println(inputSheets.size() + " sheet definitions loaded....");
	}
	
	public static List<SheetBD> getInputSheets(){
		return inputSheets;
	}
	
	public static List<SheetBD> getOutputSheets(){
		return outputSheets;
	}
	

	/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws URISyntaxException 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException, URISyntaxException {
		WorkBookManager.init("");
		System.out.println(WorkBookManager.getInputSheets().size());
		

	}

}
